<?php

namespace Hx\Http;

/**
 * 请求信息处理类
 * Class Request
 * @package Hx\Http
 * @method string ip
 * @method string method
 * @method string host
 * @method string baseUrl
 * @method string schema
 * @method string userAgent
 * @method string refer
 */
class Request {

    /**
     * 魔术方法，用属性获取参数
     * @param string $name
     * @return null
     */
    public function __get($name) {
        return $this->__call($name, NULL);
    }

    /**
     * 魔术方法，用method获取参数
     * @param string $name
     * @param array $args
     * @return null|string
     */
    public function __call($name, $args) {
        if (!$this->{$name}) {
            $method = 'get' . ucfirst($name);
            if (!method_exists($this, $method)) {
                return NULL;
            }
            $this->{$name} = $this->$method($args);
        }
        return $this->{$name};
    }

    /**
     * 获取ip
     * @return string
     */
    protected function getIp() {
        if (!empty($_SERVER['HTTP_CLIENT_IP'])) {
            //如果变量是非空或非零的值，则 empty()返回 FALSE。
            $ip = explode(',', $_SERVER['HTTP_CLIENT_IP']);
        } elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
            $ip = explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']);
        } elseif (!empty($_SERVER['REMOTE_ADDR'])) {
            $ip = explode(',', $_SERVER['REMOTE_ADDR']);
        } else {
            $ip[0] = 'None';
        }
        return $ip[0];
    }

    /**
     * 获取请求方式
     * @return string
     */
    protected function getMethod() {
        $method = !$_SERVER['HTTP_METHOD'] ? $_SERVER['REQUEST_METHOD'] : $_SERVER['HTTP_METHOD'];
        return strtoupper($method);
    }

    /**
     * 获取主机地址
     * @return string
     */
    protected function getHost() {
        if (isset($_SERVER['HTTP_HOST'])) {
            $host = $_SERVER['HTTP_HOST'];
        } else if (isset($_SERVER['SERVER_NAME'])) {
            $host = $_SERVER['SERVER_NAME'];
        } else {
            $host = 'none';
        }
        return $host;
    }

    /**
     * 获取基本URL
     * @return string
     */
    protected function getBaseUrl() {
        return $this->getSchema() . '://' . $this->getHost() . str_replace('index.php', '', $_SERVER['PHP_SELF']);
    }

    /**
     * 获取http或https
     * @return string
     */
    protected function getSchema() {
        return $_SERVER ["HTTPS"] == "on" ? 'https' : 'http';
    }

    /**
     * 获取客户端标识
     * @return mixed
     */
    protected function getUserAgent() {
        return $_SERVER['HTTP_USER_AGENT'];
    }

    /**
     * 获取上一次访问
     * @return mixed
     */
    protected function getRefer() {
        return $_SERVER['HTTP_REFERER'];
    }

    //---------------------------------

    /**
     * 获取头信息
     * @param string $key
     * @return mixed
     */
    public function getHeader($key) {
        $name = 'HTTP_' . strtoupper(str_replace('-', '_', $key));
        return $_SERVER[$name];
    }

    /**
     * 获取SERVER信息
     * @param string $key
     * @return mixed
     */
    public function getServer($key) {
        return $_SERVER[strtoupper(str_replace('-', '_', $key))];
    }

    //-------------------------

    /**
     * 判断是不是Ajax请求
     * @return bool
     */
    public function isAjax() {
        return !empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest';
    }

}